package config;

import java.io.*;

public class CLInterface {

	Runtime rt;
	
	public CLInterface()
	{
		rt = Runtime.getRuntime();
	}
	
	public boolean exists(String path) {
		File file = new File(path);
		return file.exists();
	}
	
	public boolean rename(String path, String newName)
	{
		try {
			File file = new File(path);
			if(file.exists()) {
				file.renameTo(new File(path.substring(0, path.lastIndexOf("\\")) + "\\" + newName));
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void mkdir(String path) {
		try {
			File dir = new File(path);
			dir.mkdir();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void rmdir(String path) {
		try {
			File dir = new File(path);
			if(dir.exists()) {
			      File[] files = dir.listFiles();
			      for(int i=0; i<files.length; i++) {
			         if(files[i].isDirectory()) {
			           rmdir(files[i].getPath());
			         }
			         else {
			           files[i].delete();
			         }
			      }
			    }
			    dir.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void copy(String oPath, String newPath) {
		try {
			File original = new File(oPath);
			File newFile = new File(newPath);
			if(original.isDirectory()) {
				if(!newFile.exists()) newFile.mkdir();
				File[] files = original.listFiles();
				for(int i = 0; i < files.length; ++i) {
					copy(files[i].getPath(), newPath + "\\" + files[i].getPath().substring(files[i].getPath().lastIndexOf("\\")));
				}
			}
			else {
				InputStream in = new FileInputStream(original);
			    OutputStream out = new FileOutputStream(newFile);

			    byte[] buf = new byte[1024];
			    int len;
			    while ((len = in.read(buf)) > 0) {
			        out.write(buf, 0, len);
			    }
			    in.close();
			    out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String regQuery(String key, String value, int mode) {
		String result = "";
		try {
			Process pr = rt.exec("reg query " + key + " /v " + value);
			BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line;
			while((line = input.readLine()) != null) {
				result = result + line + '\n';
			}
			pr.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(mode == 0) return result.substring(result.lastIndexOf("0x") + 2, result.indexOf('\n', result.lastIndexOf("0x")));
		return result.substring(result.lastIndexOf(":") - 1, result.indexOf('\n', result.lastIndexOf(":")));
		
	}
	
	public void regedit(String key, String value, String content) {
		try {
			Process pr = rt.exec("batch\\rg.bat " + key + " " + value + " " + content);
			pr.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void startHl2dm(int width, int height, String dir, int dxlevel) {
		try {
			Process pr = rt.exec("\"" + dir + "\\Steam.exe\"" + " -applaunch 320 -dxlevel " + dxlevel + " -novid -noborder -noforcedmparms -noforcemaccel -noforcemspd -console -high -noipx -nojoy -sw -w " + width + " -h " + height);
			pr.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean isRunning(String prname) {
        boolean found = false;
        File file = new File("batch\\procchk.vbs");
        try {
            Process p = rt.exec("cscript //NoLogo " + "\"" + file.getPath() + "\" " + prname);
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            line = input.readLine();
            if (line != null) {
                if (line.equals(prname)) {
                    found = true;
                }
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return found;
    }
	
	public void generatePreview(String skyboxfile) throws IOException {
		try {
			Process pr = rt.exec("\"vtfcmd\\VTFCmd.exe\" -file \"skybox\\" + skyboxfile + "\" -output \"skybox\" -exportformat \"png\"");
			pr.waitFor();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
